//
//  WBStatusViewModel.swift
//  Weibo
//
//  Created by kobe on 2016/12/26.
//  Copyright © 2016年 kobe. All rights reserved.
//

import Foundation

class WBStatusViewModel:CustomStringConvertible {
    
    /// 微博模型
    var status:WBStatus
    
    var memberIcon:UIImage?
    
    var vipIcon:UIImage?
    
    var retweetedStr:String?
    
    var commentsStr:String?
    
    var likeStr:String?
    
    var pictureSize = CGSize()
    
    /// 如果是被转发微博 原创微博一定没有图
    var picURLs:[WBStatusPicture]? {
        
        // 如果有转发的微博 返回被转发微博的配图  如果没有被转发的配图 返回原创微博的配图 如果都没有 返回nil
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    /// 被转发微博文字
    var retweetedText:String?
    
    var rowHeight:CGFloat = 0
    
    init(model:WBStatus) {
        self.status = model
        
        //common_icon_membership_level6
        
        
        guard let level = model.user?.mbrank else {
            return
        }
        
        if level > 0 && level < 7 {
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            
            memberIcon = UIImage(named: imageName)
            
        }
        
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            vipIcon = UIImage(named:"avatar_enterprise_vip_lv1")
        case 200:
            vipIcon = UIImage(named:"avatar_grassroot")
        default:
            break
        }
        
        //底部
//        model.reposts_count = Int(arc4random_uniform(100000))
        retweetedStr = countStr(count: model.reposts_count, defaultStr: "转发")
        commentsStr = countStr(count: model.comments_count, defaultStr: "评论")
        likeStr = countStr(count: model.attitudes_count, defaultStr: "赞")
        
        //计算配图视图大小(有原创的就计算原创的 有转发的就计算转发的)
        pictureSize = calcPictureViewSize(count: picURLs?.count)
        
        retweetedText = "@" + (status.retweeted_status?.user?.screen_name ?? "") + ":" + (status.retweeted_status?.text ?? "")
        
        updateRowHeight()
        
    }
    
    /// 给定一个数字 返回队形的描述结果
    ///
    /// - parameter count:      数字
    /// - parameter defaultStr: 默认字符串
    ///
    /// - returns: 具体数字
    fileprivate func countStr(count:Int,defaultStr:String) -> String {
        if count == 0 {
            return defaultStr
        }
        
        if count < 10000 {
            return count.description
        }
        
        return String(format:"%.02f万",Double(count)/10000)
    }
    
    fileprivate func calcPictureViewSize(count:Int?) -> CGSize {
        
        if count == 0 || count == nil {
            return CGSize()
        }
        
        
        //计算行数
        let row = (count! - 1 )/3 + 1
        
        //根据行数算高度
        let height = WBStatusPictureViewOutterMargin + CGFloat(row) * WBStatusPictureItemWidth + CGFloat(row - 1) * WBStatusPictureViewinnerMargin
        
        
        
        
        return CGSize(width: 100, height: height)
    }
    
    var description: String {
        return status.description
    }
    
    ///  使用单个图像 更新配图的视图大小
    ///
    /// - parameter image: 图片
    func updateSingleImage(image:UIImage) {
        var size = image.size
        
        //过宽图像处理
        let maxWidth:CGFloat = 300
        let minWidth:CGFloat = 40
        
        if size.width > maxWidth {
            
            size.width = maxWidth
            //等比例了 调整高度
            size.height = size.width * image.size.height / image.size.width
        }
        
        if size.width < minWidth {
            size.width = minWidth
            size.height = size.width * image.size.height / image.size.width / 4
        }
        
        // 特例 有些图像本身就很窄 很长 -> 定义一个minHeight 思路同上
        
        
        
        
        //尺寸需要增加顶部的12个点
        size.height += WBStatusPictureViewOutterMargin
        
        pictureSize = size
        
        updateRowHeight()
        
    }
    
    
    /// 根据当前视图模型内容计算行高
    fileprivate func updateRowHeight() {
        
        let margin:CGFloat = 12
        let iconHeight:CGFloat = 34
        let toolbarHeight:CGFloat = 35
        let viewSize = CGSize(width: UIScreen.cz_screenWidth() - 2 * margin, height: CGFloat(MAXFLOAT))
        let orignalFont = UIFont.systemFont(ofSize: 15)
        let retweetedFont = UIFont.systemFont(ofSize: 14)
        
        var height:CGFloat = 0
        // 1.计算顶部位置
        height = 2 * margin + iconHeight + margin
        
        // 2.正文的高度
        if let text = status.text {
            
            height += (text as NSString).boundingRect(with: viewSize,
                                            options: [.usesLineFragmentOrigin],
                                            attributes: [NSFontAttributeName:orignalFont],
                                            context: nil).height
            
        }
        
        // 3.判断是否转发微博
        if status.retweeted_status != nil {
            height += 2 * margin
            
            if let text = retweetedText {
                height += (text as NSString).boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], attributes: [NSFontAttributeName:retweetedFont], context: nil).height
            }
        }
        
        // 4.配图视图
        height += pictureSize.height
        height += margin
        
        height += toolbarHeight
        
        
        rowHeight = height
        
    }
    
}
